package org.bing.tester;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Random;

import org.bing.engine.core.domain.Console;
import org.bing.engine.utility.codec.SimpleReflectHelper;

public class RandomHelper {

    @SuppressWarnings("unchecked")
    public static <T> T random(Class<T> clz) {
        SimpleReflectHelper helper = SimpleReflectHelper.instance(clz);
        T result = (T) helper.buildObject();
        Field[] fields = helper.getTotalFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            Object value = random(field);
            if (value == null && helper.getValue(result, field) != null) {
                continue;
            }
            helper.setValue(result, field.getName(), value);
        }
        return result;
    }

    private static Object random(Field field) {
        // * @see java.lang.Boolean#TYPE
        // * @see java.lang.Character#TYPE
        // * @see java.lang.Byte#TYPE
        // * @see java.lang.Short#TYPE
        // * @see java.lang.Integer#TYPE
        // * @see java.lang.Long#TYPE
        // * @see java.lang.Float#TYPE
        // * @see java.lang.Double#TYPE
        // * @see java.lang.Void#TYPE
        Random r = new Random();
        Class<?> clz = field.getType();
        String name = clz.getName();
        if (clz == Boolean.class || name == "boolean") {
            return System.currentTimeMillis() / 2 == 0;
        } else if (clz == Character.class || name == "char") {
            return 32 + r.nextInt(64);
        } else if (clz == Byte.class || name == "byte") {
            return 1 + r.nextInt(127);
        } else if (clz == Short.class || name == "short") {
            return r.nextInt(32767);
        } else if (clz == Integer.class || name == "int") {
            return r.nextInt();
        } else if (clz == Long.class || name == "long") {
            return r.nextLong();
        } else if (clz == Float.class || name == "float") {
            return r.nextFloat();
        } else if (clz == Double.class || name == "double") {
            return r.nextDouble();
        } else if (clz == String.class) {
            return "" + r.nextDouble() + "--" + r.nextLong();
        } else if (clz == Date.class) {
            return new Date();
        }
        return null;
    }

    public static void main(String[] args) {
        RandomHelper.random(Console.class);
    }
}
